<template>
  <div>
    <!-- 表单 -->
    <el-form :label-position="labelPosition" size="small" :model="form" :inline="true" label-width="80px">
      <el-form-item label="校 区" style="font-weight:bold;">
        <el-select v-model="form.campusId" placeholder="请选择校区" style="width:150px;" clearable>
          <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼 宇" style="font-weight:bold;">
        <el-select v-model="form.buildId" placeholder="请选择楼宇" style="width:150px;" clearable>
          <el-option v-for="item in buildName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" style="font-weight:bold;">
        <el-input v-model="form.userName" placeholder="请输入学生姓名" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="学 号" style="font-weight:bold;">
        <el-input v-model="form.userNum" placeholder="请输入学号" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="性 别" style="font-weight:bold;">
        <el-select v-model="form.sex" placeholder="请选择性别" style="width:150px;" clearable>
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年 级" style="font-weight:bold;">
        <el-input v-model="form.grade" placeholder="(如2021级)" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="院 系" style="font-weight:bold;">
        <el-select v-model="form.collegeId" placeholder="请选择院系" style="width:150px;" clearable>
          <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间类型" style="font-weight:bold;">
        <el-select v-model="form.dormType" placeholder="请选择房间类型" style="width:150px;" clearable>
          <el-option label="学生宿舍" value="0"></el-option>
          <el-option label="博士宿舍" value="1"></el-option>
          <!-- <el-option label="公用房" value="2"></el-option> -->
          <!-- <el-option label="爱心房" value="3"></el-option> -->
          <!-- <el-option label="工具房" value="4"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="学生类型" style="font-weight:bold;">
        <el-select v-model="form.type" placeholder="请选择学生类型" style="width:150px;" clearable>
          <el-option label="本科生" value="0"></el-option>
          <el-option label="硕士生" value="1"></el-option>
          <el-option label="博士生" value="2"></el-option>
          <el-option label="留学生" value="3"></el-option>
          <el-option label="交换生" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间号" style="font-weight:bold;">
        <el-input v-model="form.roomNum" placeholder="(如304)" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="住宿状态" style="font-weight:bold;">
        <el-select v-model="form.liveState" placeholder="请选择住宿状态" style="width:150px;" clearable>
          <el-option label="已退宿" value="1"></el-option>
          <el-option label="住宿中" value="0"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 表单 -->

    <!-- 按钮 -->
    <div class="buttonList">
      <div class="buttonList_left" v-show="clientRole != 5">
        <el-button size="small" type="success" icon="el-icon-download" @click="downLoadAccommodation"
          plain>下载住宿信息模板</el-button>
        <el-upload action="#" :before-upload="beforeUpload" ref="newuploadin" :show-file-list="false"
          :http-request="uploadHttpRequestIn" :limit="3" :file-list="fileListIn">
          <el-button size="small" type="primary" plain icon="el-icon-upload2">批量上传住宿信息</el-button>
        </el-upload>
        <el-upload action="#" :before-upload="beforeUpload" ref="newuploadout" :show-file-list="false"
          :http-request="uploadHttpRequestOut" :limit="3" :file-list="fileListOut">
          <el-button size="small" type="primary" plain icon="el-icon-upload2">批量上传退宿信息</el-button>
        </el-upload>
        <el-button size="small" type="success" plain icon="el-icon-download"
          @click="exportAccommodation">导出学生住宿信息</el-button>
      </div>
      <div>
        <el-button size="small" class="search_botton" type="primary" icon="el-icon-search"
          @click="getDataList">查询</el-button>
        <el-button size="small" class="reset_botton" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
    </div>
    <!-- 按钮 -->

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" :data="tableData" border style="width: 100%">
      <el-table-column prop="campusName" label="校区" align="center"></el-table-column>
      <el-table-column prop="userNum" label="学号" align="center"></el-table-column>
      <el-table-column prop="userName" label="姓名" align="center"></el-table-column>
      <el-table-column prop="sex" label="性别" align="center" width="50">
        <template slot-scope="scope">
          <span v-if="(scope.row.sex == 0)">女</span>
          <span v-if="(scope.row.sex == 1)">男</span>
        </template>
      </el-table-column>
      <el-table-column prop="collegeName" label="院系" align="center"></el-table-column>
      <el-table-column prop="grade" label="年级" align="center" width="70"></el-table-column>
      <el-table-column prop="scholasticYear" label="学年制" align="center" width="70"></el-table-column>
      <el-table-column prop="type" label="学生类型" align="center" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">本科生</span>
          <span v-if="scope.row.type == 1">硕士生</span>
          <span v-if="scope.row.type == 2">博士生</span>
          <span v-if="scope.row.type == 3">留学生</span>
          <span v-if="scope.row.type == 4">交换生</span>
        </template>
      </el-table-column>
      <el-table-column prop="buildName" label="楼宇" align="center"></el-table-column>
      <el-table-column prop="roomNum" label="宿舍信息" align="center" width="80"></el-table-column>
      <el-table-column prop="peoNum" label="可住人数" align="center" width="80"></el-table-column>
      <el-table-column prop="liveState" label="住宿状态" align="center" width="90">
        <template slot-scope="scope">
          <el-button size="mini" type="warning" plain v-if="(scope.row.liveState == 1)" disabled>退宿</el-button>
          <el-button size="mini" type="primary" plain v-if="(scope.row.liveState == 0)" disabled>住宿中</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="bedCode" label="床位号" align="center"></el-table-column> -->

      <el-table-column prop="dormType" label="房间类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.dormType == 0">学生宿舍</span>
          <span v-if="scope.row.dormType == 1">博士宿舍</span>
          <span v-if="scope.row.dormType == 2">公用房</span>
          <span v-if="scope.row.dormType == 3">爱心房</span>
          <span v-if="scope.row.dormType == 4">工具房</span>
        </template>
      </el-table-column>
      <el-table-column prop="inTime" label="入住时间" align="center" width="100"></el-table-column>
      <el-table-column prop="outTime" label="退宿时间" align="center" width="100"></el-table-column>
      <el-table-column prop="operator" label="操作人" align="center"></el-table-column>
      <el-table-column prop="message" label="备注" align="center"></el-table-column>
      <!-- <el-table-column prop="handle" label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain
            @click="getAccommodationDetail({ id: scope.row.id })">查看详情</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 表格 -->

    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>


    <!-- 查看详情弹窗 -->
    <!-- <el-dialog title="学生住宿信息详情" :visible.sync="dialogFormVisible">
      <el-form :model="formDetail" :label-position="labelPosition" label-width="100px" style="margin-left:20%;"
        disabled>
        <el-form-item label="姓名 :">
          <el-input v-model="formDetail.userName" style="width:60%" value="123"></el-input>
        </el-form-item>
        <el-form-item label="学号 :">
          <el-input v-model="formDetail.userNum" style="width:60%"></el-input>
        </el-form-item>
        <el-form-item label="年级 :">
          <el-input v-model="formDetail.grade" style="width:60%"></el-input>
        </el-form-item>
        <el-form-item label="院系 :">
          <el-input v-model="formDetail.collegeName" style="width:60%"></el-input>
        </el-form-item> -->
    <!-- <el-form-item label="专业 :" >
          <el-input v-model="formDetail" style="width:60%" ></el-input>
        </el-form-item> -->
    <!-- <el-form-item label="住宿状态 :">
          <el-input v-model="formDetail.liveState" style="width:60%"></el-input>
        </el-form-item>
        <el-form-item label="入住时间 :">
          <el-input v-model="formDetail.inTime" style="width:60%"></el-input>
        </el-form-item>
        <el-form-item label="退宿时间 :">
          <el-input v-model="formDetail.outTime" style="width:60%"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog> -->
    <!-- 查看详情弹窗 -->
  </div>
</template>

<script>
import {
  getAccommodationList,
  getAccommodationDetail,
  downLoadAccommodation,
  exportAccommodation,
  uploadAccommodationIn,
  uploadAccommodationOut
} from '_api/DormManage/Accommodation/index.js'
import { getCampusName, getBuildingName } from '_api/DormManage/Building/index.js'
import { getCollegeName } from '_api/DormManage/College/index.js'
// import { getDepartmentName } from '_api/DormManage/Department/index.js'
export default {
  data() {
    return {
      labelPosition: "right",
      dialogFormVisible: false,
      form: {
        buildId: "",
        campusId: "",
        collegeId: "",
        current: 0,
        dormType: "",
        field: "",
        grade: "",
        liveState: "",
        order: "",
        pageSize: 10,
        roomNum: "",
        type: "",
        userName: "",
        userNum: "",
        sex: ""
      },
      tableData: [],
      formDetail: {},
      CampusName: [],
      CollegeName: [],
      buildName: [],
      fileListIn: [],
      fileListOut: [],
      timeNow: "",
      total: 0,
      clientRole: "",
      currentRow: null
    }
  },
  watch: {
    form: {
      // 执行方法
      handler(oldValue, newValue) {
        getBuildingName({ campusId: newValue.campusId, type: 0 }).then(res => {
          if (res.status == 200) {
            this.buildName = res.data.data
          }
        })
      },
      deep: true, // 深度监听
      // immediate: true  // 第一次改变就执行
    }
  },
  methods: {
    handleSelectionChange() {
    },
    choiceBuilding() {
      console.log(123)
    },
    //重置
    reset() {
      this.form = {
        buildId: "",
        campusId: "",
        collegeId: "",
        current: 0,
        dormType: "",
        field: "",
        grade: "",
        liveState: "",
        order: "",
        pageSize: 10,
        roomNum: "",
        type: "",
        userName: "",
        userNum: ""
      }
      this.getAccommodationList()
    },
    // 导入住宿信息
    uploadHttpRequestIn(data) {
      let fd = new FormData()
      fd.append('file', data.file)
      const loading = this.$loading({
        lock: true,
        text: '数据导入中,请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      uploadAccommodationIn(fd).then(res => {
        if (res.data.success == true) {
          loading.close();
          this.$message.success("导入成功！")
          this.reset()
        } else {
          loading.close();
          this.$message({
            message: res.data.message,
            duration: 3000
          })
        }
      }).catch(error => {
        loading.close();
        this.$message({
          message: "数据过多,请适当删减！",
          duration: 3000
        })
      })
    },
    // 导入退宿信息
    uploadHttpRequestOut(data) {
      console.log(data)
      let fd = new FormData()
      fd.append('file', data.file)
      uploadAccommodationOut(fd).then(res => {
        if (res.data.success == true) {
          this.$message.success("导入成功！")
          this.reset()
        } else {
          this.$message({
            message: res.data.message,
            duration: 3000
          })
        }
      })

    },
    beforeUpload(file) {
      // 校验
      let legalName = ["xlsx", "xls"];
      // 拿到后缀名
      let name = file.name.substring(
        file.name.lastIndexOf(".") + 1,
        file.name.length
      );
      if (legalName.includes(name)) {
      } else {
        this.$message.info("文件格式不对,仅限xls和xlsx");
        return false;
      }
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
      // console.log(_this.gettime)
      this.timeNow = _this.gettime
    },
    // 下载住宿信息模板
    downLoadAccommodation() {
      downLoadAccommodation().then(res => {
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '住宿信息模板_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('下载成功！')
      })
    },
    // 导出住宿信息
    exportAccommodation() {
      exportAccommodation(this.form).then(res => {
        // console.log(res.data,"121212121")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '住宿信息报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    // 查看住宿详情
    // getAccommodationDetail(e) {
    //   getAccommodationDetail(e).then(res => {
    //     // console.log(res)
    //     this.dialogFormVisible = true
    //     if (res.status == 200) {
    //       this.formDetail = res.data.data
    //       if (this.formDetail.liveState == 0) {
    //         this.formDetail.liveState = "退宿"
    //       } else {
    //         this.formDetail.liveState = "住宿中"
    //       }

    //       var inTime = []
    //       if (this.formDetail.inTime != "" && this.formDetail.inTime != null) {
    //         this.formDetail.inTime.forEach(item => {
    //         if (item <= 9) {
    //           item = "0" + item
    //         }
    //         inTime.push(item)
    //       })
    //       this.formDetail.inTime = inTime[0] + "-" + inTime[1] + "-" + inTime[2] + " " + inTime[3] + ":" + inTime[4] + ":" + inTime[5]
    //       }

    //       var outTime = []
    //       if (this.formDetail.outTime != "" && this.formDetail.outTime != null) {
    //         this.formDetail.outTime.forEach(item => {
    //         if (item <= 9) {
    //           item = "0" + item
    //         }
    //         outTime.push(item)
    //       })
    //       this.formDetail.outTime = outTime[0] + "-" + outTime[1] + "-" + outTime[2] + " " + outTime[3] + ":" + outTime[4] + ":" + outTime[5]
    //       // console.log(this.formDetail)
    //       }
    //     }
    //   })
    // },
    // prev(e) {
    //   //上一页
    //   this.form.current = e - 1
    //   // console.log(e-1)
    //   this.getAccommodationList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e - 1
    //   // console.log(e-1)
    //   this.getAccommodationList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = (e - 1) * 10
      this.getAccommodationList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getAccommodationList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getAccommodationList()
    },
    //  查询住宿列表
    getAccommodationList() {
      getAccommodationList(
        this.form
      ).then(res => {
        if (res.data.success == true) {
          this.getCampusName()
          this.getCollegeName()
          this.getBuildingName()
          res.data.data.list.forEach(item => {
            item.roomNum = item.roomNum + item.bedCode
          })
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
    //获取校区名称
    getCampusName() {
      getCampusName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CampusName = res.data.data
        }
      })
    },
    //获取学院名称
    getCollegeName() {
      getCollegeName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CollegeName = res.data.data
        }
      })
    },
    //获取楼宇名称
    getBuildingName() {
      getBuildingName({ type: 0 }).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.buildName = res.data.data
        }
      })
    }
  },
  created() {
    this.clientRole = window.localStorage.getItem('clientRole')
    this.getAccommodationList()
    // this.getCampusName()
    // this.getCollegeName()
    // this.getBuildingName()
  },
}
</script>
<style lang="scss" scoped>
.search_botton {
  margin-left: 30px;
}

.reset_botton {
  margin-left: 10px;
}

.buttonList {
  display: flex;
  width: 80%;
  height: 32px;
  justify-content: space-between;
}

.buttonList_left {
  margin-left: 80px;
  display: flex;
  width: 600px;
  justify-content: space-between;
}

.pagination {
  float: right;
  margin-top: 10px;
}
</style>
